myApp.controller('addColor', ['$scope', '$http', '$color', function($scope, $http, $color){
	$scope.colorName = "";
	$scope.colorDescription = "无";
	$scope.addColor = function() {
		if($scope.colorName === "") {
			alert("请填写需要添加的颜色名称！");
		}else {
			var colorData = {"color.name": $scope.colorName, "color.description": $scope.colorDescription};

			var postCfg = {
				headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
				transformRequest: function (data) {
					return $.param(data);
				}
			};

			$http.post('addColor', colorData, postCfg).then(function successCallback(response) {
				alert("已添加成功！");
				$scope.colorName = "";
				$scope.colorDescription = "无";

				var promise = $color.loadAllColor();
				promise.then(function(data) {
					$color.allColor = data;
				}, function(n) {
					$color.allColor = [{"name": "数据加载出错！"}];
				});
				console.log("ok：" + response);
			}, function errorCallback(response) {
				console.log("fail：" + response);
			});
		}
	};
}]);

myApp.controller('deleteColor', ['$scope', '$http', '$color', function($scope, $http, $color){
	
	$scope.colorId = "";
	$scope.deleteColor = function() {
		if ($scope.colorId === "" || $scope.colorId === null) {
			alert("请选择需要删除的颜色！");
		}else {
			var colorData = {"color.colorId": $scope.colorId};

			var postCfg = {
				headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
				transformRequest: function (data) {
					return $.param(data);
				}
			};

			$http.post('deleteColor', colorData, postCfg).then(function successCallback(response) {
				alert("已删除成功！");
				$scope.colorId = "";

				var promise = $color.loadAllColor();
				promise.then(function(data) {
					$color.allColor = data;
				}, function(n) {
					$color.allColor = [{"name": "数据加载出错！"}];
				});
				console.log("ok：" + response);
			}, function errorCallback(response) {
				console.log("fail：" + response);
			});
		}
		
	};
}]);

myApp.controller('modifyColor', ['$scope', '$http', '$color', function($scope, $http, $color){
	
	$scope.colorId = "";
	$scope.colorName = "";
	$scope.colorDescription = "无";
	$scope.modifyColor = function() {
		if($scope.colorId === "" || $scope.colorId === null) {
			alert("请选择需要修改的颜色！");
		}else if ($scope.colorName === "") {
			alert("请填写颜色名称！");
		}else {
			var colorData = {"color.colorId": $scope.colorId, "color.name": $scope.colorName, "color.description": $scope.colorDescription};
			var postCfg = {
				headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
				transformRequest: function (data) {
					return $.param(data);
				}
			};

			$http.post('modifyColor', colorData, postCfg).then(function successCallback(response) {
				alert("已修改成功！");
				$scope.colorId = "";
				$scope.colorName = "";
				$scope.colorDescription = "无";

				var promise = $color.loadAllColor();
				promise.then(function(data) {
					$color.allColor = data;
				}, function(n) {
					$color.allColor = [{"name": "数据加载出错！"}];
				});
				console.log("ok：" + response);
			}, function errorCallback(response) {
				console.log("fail：" + response);
			});
		}
		
	};
}]);